package com.eshop.modules.template.rest;

import java.lang.invoke.SerializedLambda;
import org.springframework.cache.annotation.Cacheable;
import com.eshop.modules.template.domain.SystemCity;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import java.util.Iterator;
import java.util.List;
import java.io.Serializable;
import com.eshop.exception.BadRequestException;
import com.eshop.modules.product.domain.StoreProduct;
import java.util.Arrays;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.modules.aop.ForbidSubmit;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.template.service.dto.ShippingTemplatesDto;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import java.io.IOException;
import com.eshop.modules.template.service.dto.YxShippingTemplatesDto;
import com.eshop.modules.template.service.dto.ShippingTemplatesQueryCriteria;
import javax.servlet.http.HttpServletResponse;
import com.eshop.modules.product.service.StoreProductService;
import com.eshop.dozer.service.IGenerator;
import com.eshop.modules.template.service.SystemCityService;
import com.eshop.modules.template.service.ShippingTemplatesService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "运费模板管理" })
@RestController
@RequestMapping({ "/api/yxShippingTemplates" })
public class ShippingTemplatesController
{
    private final ShippingTemplatesService shippingTemplatesService;
    private final SystemCityService systemCityService;
    private final IGenerator generator;
    private final StoreProductService storeProductService;
    
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('admin','yxShippingTemplates:list')")
    public void download(final HttpServletResponse response, final ShippingTemplatesQueryCriteria criteria) throws IOException {
        this.shippingTemplatesService.download(this.generator.convert(this.shippingTemplatesService.queryAll(criteria), (Class)YxShippingTemplatesDto.class), response);
    }
    
    @GetMapping
    @Log("查询运费模板")
    @ApiOperation("查询运费模板")
    @PreAuthorize("@el.check('admin','yxShippingTemplates:list')")
    public ResponseEntity<Object> getYxShippingTemplatess(final ShippingTemplatesQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.shippingTemplatesService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @ForbidSubmit
    @PostMapping({ "/save/{id}" })
    @Log("新增/保存运费模板")
    @ApiOperation("新增/保存运费模板")
    @PreAuthorize("hasAnyRole('admin','yxShippingTemplates:add','yxShippingTemplates:edit')")
    public ResponseEntity<Object> create(@PathVariable final Integer id, @Validated @RequestBody final ShippingTemplatesDto shippingTemplatesDto) {
        this.shippingTemplatesService.addAndUpdate(id, shippingTemplatesDto);
        return new ResponseEntity(HttpStatus.CREATED);
    }

    @ForbidSubmit
    @Log("删除运费模板")
    @ApiOperation("删除运费模板")
    @PreAuthorize("@el.check('admin','yxShippingTemplates:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        List<StoreProduct> productList = this.storeProductService.list();
        Arrays.asList(ids).forEach((id) -> {
            Iterator var3 = productList.iterator();

            StoreProduct storeProduct;
            do {
                if (!var3.hasNext()) {
                    this.shippingTemplatesService.removeById(id);
                    return;
                }

                storeProduct = (StoreProduct)var3.next();
            } while(!id.equals(storeProduct.getTempId()));

            throw new BadRequestException("运费模板存在商品关联，请删除对应商品");
        });
        return new ResponseEntity(HttpStatus.OK);
    }
    
    @Cacheable(cacheNames = { "yshop:city_list" })
    @GetMapping({ "/citys" })
    public ResponseEntity<Object> cityList() {
        final List<SystemCity> cityList = (List<SystemCity>)this.systemCityService.list(Wrappers.<SystemCity>lambdaQuery().eq(SystemCity::getParentId, 0));
        for (final SystemCity systemCity : cityList) {
            final List<SystemCity> children = (List<SystemCity>)this.systemCityService.list(Wrappers.<SystemCity>lambdaQuery().eq(SystemCity::getParentId, systemCity.getCityId()));
            systemCity.setChildren((List)children);
        }
        return new ResponseEntity(cityList, HttpStatus.OK);
    }
    
    public ShippingTemplatesController(final ShippingTemplatesService shippingTemplatesService, final SystemCityService systemCityService, final IGenerator generator, final StoreProductService storeProductService) {
        this.shippingTemplatesService = shippingTemplatesService;
        this.systemCityService = systemCityService;
        this.generator = generator;
        this.storeProductService = storeProductService;
    }
}
